/**********************************************************

     文件名称: read_infor.c

     项目功能: 

     作者: 佳奇

     邮箱: 1374804492@qq.com 

     创建时间: Fri 26 Aug 2022 08:35:58 PM CST

**********************************************************/


#include <stdio.h>
#include <stdlib.h>
#include "sup_mgr.h"


void read_infor(cas_list* s1, com_list* s2, sales_list* s3, admin* s4)
{
	FILE* fp1 = fopen("cas_infor", "rb");
	FILE* fp2 = fopen("com_infor", "rb");
	FILE* fp3 = fopen("sales_infor", "rb");
	FILE* fp4 = fopen("admin_infor", "rb");

	if((fp1 == NULL) || (fp2 == NULL) || (fp3 == NULL) || (fp4 == NULL))
	{
		perror("fopen error!");
		return;
	}

	//	将收银员信息从文件加载至链表
	while(1)
	{
		cas_list* n = (cas_list*) malloc (sizeof(cas_list));
		cas* demo1 = (cas*) malloc (sizeof(cas));
		if(fread(demo1, sizeof(cas), 1, fp1) == 0) break;

		if(NULL == n || NULL == demo1)
		{
			perror("malloc error!");
			return;
		}

		n->data = demo1;
		n->next = s1->next;
		s1->next = n;
	}

	//	将商品信息从文件加载至链表
	while(1)
	{
		com_list* n = (com_list*) malloc (sizeof(com_list));
		com* demo1 = (com*) malloc (sizeof(com));
		if(fread(demo1, sizeof(com), 1, fp2) == 0) break;

		if(NULL == n || NULL == demo1)
		{
			perror("malloc error!");
			return;
		}

		n->data = demo1;
		n->next = s2->next;
		s2->next = n;
	}

	//	将销售记录信息从文件加载至链表
	while(1)
	{
		sales_list* n = (sales_list*) malloc (sizeof(sales_list));
		sales* demo1 = (sales*) malloc (sizeof(sales));
		if(fread(demo1, sizeof(sales), 1, fp3) == 0) break;

		if(NULL == n || NULL == demo1)
		{
			perror("malloc error!");
			return;
		}

		n->data = demo1;
		n->next = s3->next;
		s3->next = n;
	}

	//	将用户密码信息从文件加载至链表
	while(1)
	{
		admin* n = (admin*) malloc (sizeof(admin));
		cry* demo1 = (cry*) malloc (sizeof(cry));
		if(fread(demo1, sizeof(cry), 1, fp4) == 0) break;

		if(NULL == n || NULL == demo1)
		{
			perror("malloc error!");
			return;
		}

		n->data = demo1;
		n->next = s4->next;
		s4->next = n;
	}


	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	fclose(fp4);
}

